package com.xiaobai.zhuangxiuguanjia.activities;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.xiaobai.zhuangxiuguanjia.R;
import com.xiaobai.zhuangxiuguanjia.Utils.RequestManager;
import com.xiaobai.zhuangxiuguanjia.adapters.RecommendAppListAdapter;
import com.xiaobai.zhuangxiuguanjia.model.AppData;
import com.xiaobai.zhuangxiuguanjia.model.RecommendAppInfo;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecommendAppListActivity extends AppCompatActivity {

    private static final String url = "http://mobileapi.to8to.com/smallapp.php?model=recommendapp&action=list&appid=1&appversion=2.5.1&systemversion=16&appostype=1&to8to_token=&channel=%E5%BA%94%E7%94%A8%E5%AE%9D&version=2.5&";

    private List<AppData> appDataList;
    private RecommendAppListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend_app_list);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("精品推荐");

        appDataList = new ArrayList<>();
        RequestQueue requestQueue = RequestManager.getInstance(this).getRequestQueue();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("response", response.toString());
                        RecommendAppInfo recommendAppInfo = new RecommendAppInfo();
                        recommendAppInfo.parseJson(response);
                        List<AppData> appDatas = recommendAppInfo.getAppDataList();
                        Log.d("size", "appDatas: " + appDatas.size());
                        appDataList.clear();
                        appDataList.addAll(appDatas);

                        for (int i = 2; i < appDataList.size() - 1; i++) {
                            String appname = appDataList.get(i).getAppname();
                            for (int j = i + 1; j < appDataList.size(); j++) {
                                String appname1 = appDataList.get(j).getAppname();
                                if (appname.equals(appname1))
                                    appDataList.remove(j);
                            }
                        }

                        appDataList.remove(0);
                        appDataList.remove(1);

                        adapter.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });
        requestQueue.add(jsonObjectRequest);

        adapter = new RecommendAppListAdapter(this, appDataList);

        Log.d("size", "size: " + appDataList.size());

        ListView appList = (ListView) findViewById(R.id.recommend_app_list);
        appList.setAdapter(adapter);
        LayoutAnimationController controller =
                new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.list_zoom_in));
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        appList.setLayoutAnimation(controller);
        appList.startLayoutAnimation();
    }
}
